package pl.enigmatic.parser.string.java.util;

import java.util.Locale;

import pl.enigmatic.parser.string.ArrayConverter;
import pl.enigmatic.parser.string.Converter;


public class LocaleConverter extends Converter<Locale> {

	private final ArrayConverter<String> array = new ArrayConverter<String>(String[].class);

	public Locale convert(final String[] params) {
		if (params.length == 1) {
			return new Locale(params[0]);
		} else if (params.length == 2) {
			return new Locale(params[0], params[1]);
		} else if (params.length >= 3) {
			return new Locale(params[0], params[1], params[2]);
		} else {
			return new Locale(null);
		}
	}

	@Override
	public Locale convert(final String value) {
		return convert(array.convert(value));
	}

	@Override
	public String convertBack(final Locale value) {
		return array.convertBack(new String[] { value.getLanguage(), value.getCountry(), value.getVariant() }).trim();
	}

}
